package com.yami.shop.api.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.dto.MsgDto;
import com.yami.shop.bean.model.Msg;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.MsgService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * (Msg)表控制层
 *
 * @author rows
 * @since 2023-04-11 09:41:36
 */
@RestController
@RequestMapping("/p/msg")
@RequiredArgsConstructor
public class MsgController {
    /**
     * 服务对象
     */
    private final MsgService msgService;

    @GetMapping("/list")
    @Operation(summary = "获取个人留言列表" , description = "获取个人留言列表")
    public ServerResponseEntity list(Page<Msg> page, MsgDto msgDto) {
        if(msgDto.getPub().intValue()==0) {
            msgDto.setUserId(SecurityUtils.getUser().getUserId());
        }
        return ServerResponseEntity.success(msgService.selectPage(page,msgDto));
    }

    /**
     * 新增数据
     *
     * @param msg 实体对象
     * @return 新增结果
     */
    @Operation(summary = "新增", description = "新增")
    @PostMapping("add")
    public ServerResponseEntity add(@Valid @RequestBody Msg msg) {
        msg.setUserId(SecurityUtils.getUser().getUserId());
        msg.setStatus(0);
        msg.setPub(0);
        return ServerResponseEntity.success(this.msgService.save(msg));
    }
}